/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bbchess.units.pieces;

import bbchess.dialogs.PawnPromotionDialog;
import bbchess.units.ChessBoard;
import bbchess.units.ChessPiece;
import bbchess.units.ChessSquare;
import bbchess.util.ImageLoader;

/**
 *
 * @author Tyler
 */
public class Pawn extends ChessPiece {

    private boolean capturableEnPassant = false;
    private String enpassantSquare1 = null;
    private String enpassantSquare2 = null;
    private boolean capturableEnPassantMove = true; // determines if this piece can be captured on a given move

    public Pawn(String color, ChessSquare position) {
        initPiece(color, position);
        setUnit("");

        if (color.equals("black")) {
            setSprite(ImageLoader.getbPawnImage());
        } else {
            setSprite(ImageLoader.getwPawnImage());
        }
    }

    public void promoteTo(String unit) {

        ChessPiece promotedPiece = null;

        // if white
        if (getColor().equals("white")) {
            if (unit.equals("Q")) {
                promotedPiece = new Queen("white", getPosition());
            } else if (unit.equals("R")) {
                promotedPiece = new Rook("white", getPosition());
            } else if (unit.equals("B")) {
                promotedPiece = new Bishop("white", getPosition());
            } else if (unit.equals("N")) {
                promotedPiece = new Knight("white", getPosition());
            }
        } // else black
        else {
            if (unit.equals("Q")) {
                promotedPiece = new Queen("black", getPosition());
            } else if (unit.equals("R")) {
                promotedPiece = new Rook("black", getPosition());
            } else if (unit.equals("B")) {
                promotedPiece = new Bishop("black", getPosition());
            } else if (unit.equals("N")) {
                promotedPiece = new Knight("black", getPosition());
            }
        }

        // add the new piece to the front of the list, so that kings
        // can adjust their moves based on everyone elses.
        board.getChessPieces().add(0, promotedPiece);

        promotedPiece.setBoard(board);
        board.getChessPieces().remove(this);
        board.validate();
        
        setLastMoves(true);
        clearLastMoves();
        board.clearSquares();
        board.updateChessMoves();
        board.adjustChessMoves();

        // check for mate
        String check = board.isCheck();

        if (check.contains("checkmate:") ||
                    check.contains("draw:")) {
                board.endGame(check);
            }

        // clear threatenedBy list
        for (ChessPiece tempPiece : board.getChessPieces()) {
            tempPiece.clearThreatenedBy();
        }
    }

    /**
     * Determines if the given pawn can capture a piece next to it via an enpassant
     * capture, and add's the appropriate capturable squares to it's capturableSquares
     * ArrayList.
     */
    public String canCaptureEnPassant() {
        // if there are opposite color pawns next to us that are capturable via en passant,
        // then add the appropriate capture square(s)

        // Initilize variables
        ChessPiece piece1 = null;
        ChessPiece piece2 = null;

        String square1 = board.calculateCapture(getPosition().getPosition(), getColor(), 1, 1, 0);
        String square2 = board.calculateCapture(getPosition().getPosition(), getColor(), 1, -1, 0);


        if (square1 != null) {
            piece1 = board.getChessSquares().get(square1).getContents();
        }

        if (square2 != null) {
            piece2 = board.getChessSquares().get(square2).getContents();
        }

        if (piece1 != null) {
            if (piece1 instanceof Pawn) {
                Pawn piece1Pawn = (Pawn) piece1;
                if (piece1Pawn.getCapturableEnPassant()) {
                    // piece1 is a pawn capturable via enpassant
                    if (getColor().equals("white")) {
                        enpassantSquare1 = ChessBoard.calculatePosition(square1, 0, 1);
                    } else {
                        enpassantSquare1 = ChessBoard.calculatePosition(square1, 0, -1);
                    }
                    return enpassantSquare1;
                }
            }
        }

        if (piece2 != null) {
            if (piece2 instanceof Pawn) {
                Pawn piece2Pawn = (Pawn) piece2;
                if (piece2Pawn.getCapturableEnPassant()) {
                    // piece2 is a pawn capturable via enpassant
                    if (getColor().equals("white")) {
                        enpassantSquare2 = ChessBoard.calculatePosition(square2, 0, 1);
                    } else {
                        enpassantSquare2 = ChessBoard.calculatePosition(square2, 0, -1);
                    }
                    return enpassantSquare2;
                }
            }
        }

        return null;
    }

    @Override
    public void tryToPromote() {
        String positionSquare = getPosition().getPosition();

        // if we can promote
        if (positionSquare.equals("a1")
                || positionSquare.equals("b1")
                || positionSquare.equals("c1")
                || positionSquare.equals("d1")
                || positionSquare.equals("e1")
                || positionSquare.equals("f1")
                || positionSquare.equals("g1")
                || positionSquare.equals("h1")
                || positionSquare.equals("a8")
                || positionSquare.equals("b8")
                || positionSquare.equals("c8")
                || positionSquare.equals("d8")
                || positionSquare.equals("e8")
                || positionSquare.equals("f8")
                || positionSquare.equals("g8")
                || positionSquare.equals("h8")) {

            board.gui.glassPane.stopDragging();
            board.getParent().repaint();
            PawnPromotionDialog pawnPromotion = new PawnPromotionDialog(board.gui, this);
            pawnPromotion.setVisible(true);
        }
    }

    @Override
    public void calculateLegalMoves() {
        // if first move
        if (getMove() == 1) {
            // if white
            if (getColor().equals("white")) {
                addLegalMoves(board.calculatePath(getPosition().getPosition(), 2, 0, 1, false));
            } // if black
            else {
                addLegalMoves(board.calculatePath(getPosition().getPosition(), 2, 0, -1, false));
            }
        } // if not first move
        else {
            // if white
            if (getColor().equals("white")) {
                addLegalMoves(board.calculatePath(getPosition().getPosition(), 1, 0, 1, false));
            } // if black
            else {
                addLegalMoves(board.calculatePath(getPosition().getPosition(), 1, 0, -1, false));
            }
        }
    }

    @Override
    public void calculateCaptureMoves() {
        
        String enpassantMove = canCaptureEnPassant();

        // add en passant capture if possible
        if (enpassantMove != null) {
            addCapturableMove(enpassantMove);
        }

        // if white
        if (getColor().equals("white")) {
            addCapturableMove(board.calculateCapture(getPosition().getPosition(), getColor(), 1, 1, 1));
            addCapturableMove(board.calculateCapture(getPosition().getPosition(), getColor(), 1, -1, 1));
        } // if black
        else {
            addCapturableMove(board.calculateCapture(getPosition().getPosition(), getColor(), 1, 1, -1));
            addCapturableMove(board.calculateCapture(getPosition().getPosition(), getColor(), 1, -1, -1));
        }
    }

    @Override
    public void calculateThreatenedMoves() {
        // if white
        if (getColor().equals("white")) {
            addThreatenedMoves(board.calculatePath(getPosition().getPosition(), 1, 1, 1, true));
            addThreatenedMoves(board.calculatePath(getPosition().getPosition(), 1, -1, 1, true));
        } // if black
        else {
            addThreatenedMoves(board.calculatePath(getPosition().getPosition(), 1, 1, -1, true));
            addThreatenedMoves(board.calculatePath(getPosition().getPosition(), 1, -1, -1, true));
        }
    }

    /**
     * Calculates if a piece can be captured by an enPassant move.
     */
    public void calculateCapturableEnPassant() {
        String positionSquare = getPosition().getPosition();
        

        // Should we be capturable by en passant?
        if (positionSquare.equals("a4")
                || positionSquare.equals("b4")
                || positionSquare.equals("c4")
                || positionSquare.equals("d4")
                || positionSquare.equals("e4")
                || positionSquare.equals("f4")
                || positionSquare.equals("g4")
                || positionSquare.equals("h4")
                || positionSquare.equals("a5")
                || positionSquare.equals("b5")
                || positionSquare.equals("c5")
                || positionSquare.equals("d5")
                || positionSquare.equals("e5")
                || positionSquare.equals("f5")
                || positionSquare.equals("g5")
                || positionSquare.equals("h5")) {

            // if we're on our second move and the opposing player hasn't already moved
            if (getMove() == 2 && capturableEnPassantMove) {
                capturableEnPassant = true;
                capturableEnPassantMove = false;
            }

            else {
                capturableEnPassant = false;
            }
        }

        else {
            capturableEnPassant = false;
        }
    }

    /**
     * True if the pawn can be captured via enpassant
     */
    public boolean getCapturableEnPassant() {
        return capturableEnPassant;
    }
}
